$(document).ready(function() {
	//偶数行
	$(".bulletins tr:even").each(function() {
		$(this).mouseover(function() {
			$(this).addClass("mouseover");
		});
		$(this).mouseout(function() {
			$(this).removeClass("mouseout");
		});		
	});
	//奇数行
	$(".bulletins tr:odd").each(function() {
		$(this).mouseover(function() {
			$(this).addClass("mouseover");
		});
		$(this).mouseout(function() {
			$(this).removeClass("mouseout");
		});		
	});
	//删除按钮
	$(".bulletins :button[name='delete']").click(function() {
		var bulletinId = $(this).attr("title");
		var isConfirmed = confirm("你确定要删除ID：" + bulletinId + "的公告吗？");
		if (isConfirmed) {
			var deleteStatus = deleteBulletin(bulletinId);
			
			if (deleteStatus) {
				$(this).parent().parent().remove();		
			}
		} 
	});
	//转到查看并修改页面
	$(".bulletins :button[name='view']").click(function() {
		var bulletinId = $(this).attr("title");
		window.location.href = "updateBulletinForm.do?id=" + bulletinId;
	});
	//修改按钮
	
	$("#bulletin :button[name='modify']").click(modifyBulletin);
	$("#bulletin :button[name='cancel']").click(function() {
		window.location.href = "bulletinList.do";
	});
});

function deleteBulletin(bulletinId) {
	var deleteStatus = false;
	$.ajax({
		type : "GET",
		aysnc : false,
		dataType : "xml",
		url : "deleteBulletin.do?id=" + bulletinId,
		success : function(xmlData, textStatus) {
			var messageDialog = $(".messageDialog");
			var response = $(xmlData);
			var status = response.find("status").text();
			var message = response.find("message").text();
			messageDialog.find("p").text(message);
			
			if (status == "true") {
				deleteStatus = true;
				messageDialog.dialog("destroy");
				messageDialog.dialog({
					autoOpen : false,
					modal : true,
					title : "删除成功",
					buttons : {
						"确定" : function()	{
							$(this).dialog("close");
						}
					}
				});
				
			} else {
				deleteStatus = false;
				messageDialog.dialog("destroy");
				messageDialog.dialog({
					autoOpen : false,
					modal : true,
					title : "删除失败",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
			}
			messageDialog.dialog("open");
		}
	});
	return new Boolean(deleteStatus);
}

function modifyBulletin() {
	var bulletin = $("#bulletin");
	var title = bulletin.find(".title").children(":last").text();
	var content = bulletin.find(".content").children(":last").text();
	var replaceTitle = "<input name=\"title\" value=\"" + title + "\"></input>";
	var replaceContent = "<textarea rows=\"5\" cols=\"25\" name=\"content\">" + content + "</textarea>";
	var replaceButton = "<input type=\"submit\" name=\"submit\" value=\"提交\"></input>";
	$("#bulletin .title").children(":last").replaceWith(replaceTitle);
	$("#bulletin .content").children(":last").replaceWith(replaceContent);
	$("#bulletin :button[name='modify']").replaceWith(replaceButton);			
	
}

